<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>计算机类</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片-->
    <el-card>
      <el-row :gutter="40">
        <el-col :span="8">
          <!--搜索-->
          <el-input
            placeholder="请输入内容 书名或者作者"
            v-model="queryInfo.query"
            clearable
            @clear="getlist"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getlist"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="dialogVisible = true"
            >添加图书</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="Id" width="80"> </el-table-column>
        <el-table-column prop="name" label="书名" width="280">
        </el-table-column>
        <el-table-column prop="author" label="作者" width="150">
        </el-table-column>
        <el-table-column prop="number" label="库存" width="70">
        </el-table-column>
        <el-table-column prop="" label="操作"> </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        layout="total, prev, pager, next"
        background
        :page-count="10"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!--添加书籍区-->
    <el-dialog title="添加图书" :visible.sync="dialogVisible" width="50%" @close="addDoalogClose">
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="图书名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="addForm.author"></el-input>
        </el-form-item>
        <el-form-item label="现有库存" prop="num">
          <el-input v-model="addForm.num"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBook"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    var checkNum = (rule, value, cb) => {
      //验证正整数正则
      const regNum = /^[+]{0,1}(\d+)$/
      if (regNum.test(value)) {
        return cb()
      }
      cb(new Error('请输入0-99999的正整数'))
    }
    return {
      tableData: [],
      total: 0,
      //向后端传参实现分页
      queryInfo: {
        query: "",
        //当前的页数
        pagenum: 1,
        //每页多少数据
        pagesize: 8,
      },
      dialogVisible: false,
      addForm: {
        name: '',
        author: '',
        num: ''
      },
      addFormRules: {
        name: [
          { required: true, message: "请输入图书名称", trigher: "blur" },
          {
            min: 1,
            max: 15,
            message: "图书名称在1-15个字符之间",
            trigher: "blur",
          },
        ],
        author: [
          {required: true, message: "请输入图书作者", trigher: "blur"},
          {
            min: 1,
            max: 15,
            message: "图书名称在1-15个字符之间",
            trigher: "blur",
          },
        ],
        num:  [
          {required: true, message: "请输入库存", trigher: "blur"},
          {
            min: 1,
            max: 15,
            message: "图书库存在0-99999之间",
            trigher: "blur",
          },{validator: checkNum, trigher: "blur"}
        ],
      },
    };
  },
  created() {
    this.getlist();
  },
  methods: {
    async getlist() {
      const { data } = await this.$http.get("getbook/getjisuanji", {
        params: this.queryInfo,
      });
      if (data.stats !== 200) return this.$message.error("数据获取失败");
      this.tableData = data.List;
      this.total = data.total;
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getlist();
    },
    addDoalogClose() {
      this.$refs.addFormRef.resetFields()
    },
    addBook() {
      this.$refs.addFormRef.validate(async valid => {
        if(!valid) return
        //发送添加请求
        const {data} = await this.$http.post('addbook',this.addForm)
        if(data.status !== 201) return this.$message.error("添加失败");
        this.$message.success("添加图书成功")
        this.dialogVisible = false
        this.getlist()
      })
    }
  },
};
</script>